package org.epidroid.epidroid.news.ui;

import java.util.List;

import org.apache.james.mime4j.codec.DecoderUtil;
import org.epidroid.epidroid.EpiDroid;
import org.epidroid.epidroid.R;
import org.epidroid.epidroid.news.engine.NewsArticle;
import org.epidroid.epidroid.news.utils.TextFormater;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TopicAdapter extends ArrayAdapter<NewsArticle> {
	private List<NewsArticle> _items = null;
	private TopicActivity _context = null;

	public TopicAdapter(TopicActivity context, int textViewResourceId,
			List<NewsArticle> objects) {
		super(context, textViewResourceId, objects);

		_items = objects;
		_context = context;
	}

	@Override
	public int getCount() {
		return _items.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.news_article, null);
		}

		final NewsArticle a = this._items.get(position);

		Button replyButton = (Button) v.findViewById(R.id.news_article_reply);
		replyButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				_context.launchReply(a);
			}
		});

		TextView header = (TextView) v.findViewById(R.id.news_article_header);
		setTitle(a, header);

		TextView content = (TextView) v.findViewById(R.id.news_article_body);

		/*
		 * The decoding of the string does not seem to be completely accurate..
		 */
		String tmpBody = null;
		try {
			tmpBody = DecoderUtil.decodeEncodedWords(a.getBody(), null);
		} catch (Error e) {
			Log.d(NewsActivity.LOGCAT, "Error: " + e.getMessage());
			tmpBody = a.getBody();
		} catch (Exception e) {
			Log.d(NewsActivity.LOGCAT, "Exception: " + e.getMessage());
			tmpBody = a.getBody();
		}

		final String body = tmpBody;

		if (null == body) {
			if (a.isLoading()) {
				content.setText("Loading...");
			} else {
				content.setText("<click to expand>");
			}
			replyButton.setVisibility(View.GONE);
		} else {
			content.setText(TextFormater.formatArticle(body));
			replyButton.setVisibility(View.VISIBLE);
		}

		/*
		 * Hide the button if it is in production mode, because the possibility
		 * to reply does not exist yet.
		 */
		if (_context.isReplying() || EpiDroid.IS_PROD) {
			replyButton.setVisibility(View.GONE);
		}

		content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ((null == body) && (!a.isLoading())) {
					/*
					 * TODO: put that in a thread
					 */
					_context.loadArticleBody(a);
				}
			}

		});

		return (v);
	}

	private void setTitle(NewsArticle a, TextView header) {

		SpannableStringBuilder builder = new SpannableStringBuilder();
		SpannableStringBuilder fromBuilder = new SpannableStringBuilder(a
				.getFrom());
		SpannableStringBuilder dateBuilder = new SpannableStringBuilder(a
				.getDate());

		fromBuilder.setSpan(new ForegroundColorSpan(Color.CYAN), 0,
				fromBuilder.length(), 0);
		dateBuilder.setSpan(new ForegroundColorSpan(0xffDDDDDD), 0,
				dateBuilder.length(), 0);

		builder.append(fromBuilder);
		builder.append("\n");
		builder.append(dateBuilder);
		header.setText(builder);
	}
}
